# 导包
import requests
# 域名
host = "https://mall.deyunce.com/"
# 登录
def login(zhi_1,zhi_2,zhi_3):
    # api地址
    api_account_login = "api/account/login"
    # url地址
    url = host+api_account_login
    # 请求头参数
    headers = {
        "Content-Type": "application/json",
        "Authorization": "Basic ZGV5dW5jZTo4MjgxMjM="
    }
    # 接口参数
    json_data = {
        "account": zhi_1,
        "password": zhi_2,
        "client": zhi_3
    }
    # 发送登录请求
    response = requests.post(url=url, json=json_data, headers=headers)
    # 输出结果
    return response
# 商品详情
def goods_getGoodsDetail(token,id):
    # api地址
    api_goods_getGoodsDetail = 'api/goods/getGoodsDetail'
    # url地址
    url = host+api_goods_getGoodsDetail
    # 请求头参数
    headers = {
        "Content-Type": "application/json",
        "Authorization": "Basic ZGV5dW5jZTo4MjgxMjM=",
        "Token": token
    }
    # 接口参数
    params = {
        "id": id
    }
    # 发送商品详情请求
    response= requests.get(url=url,params=params,headers=headers)
    # 输出结果
    return response
# 添加购物车
def cart_add(token,item_id,goods_num):
    # api地址
    api_cart_add= 'api/cart/add'
    # url地址
    url= host+api_cart_add
    # 请求头参数
    headers = {
        "Content-Type": "application/json",
        "Authorization": "Basic ZGV5dW5jZTo4MjgxMjM=",
        "Token": token
    }
    # 接口参数
    json_data= {
        "item_id": item_id,
        "goods_num": goods_num
    }
    response = requests.post(url=url,json=json_data,headers=headers)
    # 输出结果
    return response
# 购物车提交订单
def order_buy(token,item_id,num,action,type,delivery_type):
    # api地址
    api_order_buy='api/order/buy'
    # url地址
    url=host+api_order_buy
    # 请求头参数
    headers = {
        "Content-Type": "application/json",
        "Authorization": "Basic ZGV5dW5jZTo4MjgxMjM=",
        "Token": token
    }
    # 接口参数
    json_data={
        "goods":[
            {
                "item_id":item_id,
                "num":num
            }
        ],
        "action":action,
        "type":type,
        "address_id":2976,
        "order_source":"6",
        "delivery_type":delivery_type
    }
    # 发送提交订单请求
    response = requests.post(url=url,json=json_data,headers=headers)
    # 输出结果
    return response
# 支付订单
def payment_pcPrepay(token,order_id,order_source,pay_way):
    # api地址
    api_payment_pcPrepay='api/payment/pcPrepay'
    # url地址
    url=host+api_payment_pcPrepay
    # 请求头参数
    headers = {
        "Content-Type": "application/json",
        "Authorization": "Basic ZGV5dW5jZTo4MjgxMjM=",
        "Token": token
    }
    # 接口参数
    json_data={
        "order_id": order_id,
        "order_source": order_source,
        "pay_way": pay_way
    }
    # 发送支付订单请求
    response= requests.post(url=url,json=json_data,headers=headers)
    # 输出结果
    return response
# 调试
if __name__ == '__main__':
    # 发送登录请求
    login_response = login("19710079687", "18103945931t", 6)
    # 响应结果json化
    login_resbody = login_response.json()
    # 提取token
    token = login_resbody["data"]["token"]
    # 发送查看商品详情
    goods_getGoodsDetail_response = goods_getGoodsDetail(token, 1782)
    # 响应结果json化
    goods_getGoodsDetail_resbody= goods_getGoodsDetail_response.json()
    # 提取item_id
    item_id= goods_getGoodsDetail_resbody["data"]["goods_item"][0]["id"]
    # 发送添加购物车请求
    cart_add_response= cart_add(token,item_id,1)
    # 响应结果json化
    cart_add_resbody= cart_add_response.json()
    # 发送提交订单请求
    order_buy_response= order_buy(token,item_id,1,"submit","cart",1)
    # 响应结果json化
    order_buy_resbody= order_buy_response.json()
    # 提取order_id
    order_id= order_buy_resbody["data"]["order_id"]
    # 发送支付订单请求
    payment_pcPrepay_response= payment_pcPrepay(token,order_id,6,3)
    # 响应结果json化
    payment_pcPrepay_resbody= payment_pcPrepay_response.json()
    # 打印响应结果结果
    print(payment_pcPrepay_resbody)
    # 响应码200断言
    assert payment_pcPrepay_response.status_code == 200
    # 响应结果支付成功断言
    assert payment_pcPrepay_resbody["msg"] == "支付成功"

